/// <reference path="../_all.ts" />

module Application.Services {

    export class UserService implements Application.Interfaces.Services.IUserService {
        _userClass: ng.resource.IResourceClass;
        _userSessionClass: ng.resource.IResourceClass;

        public static $inject = [
            '$http',
            '$resource',
        ];

        constructor(public Http: ng.IHttpService, public Resource: ng.resource.IResourceService) {
            this._userClass = <Application.Resources.IUserClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('users/:id'), null, null);
            this._userSessionClass = <Application.Resources.IUserClass> this.Resource(Application.Services.ServiceUtility.GetServiceURL('users/:id/sessions'), null, null);
        }

        public GetSession(login: string, password: string, callback: Function) {
            this._userSessionClass.query({ login: login, password: password }, function (d: Application.Resources.IUser[], h: any) {
                var user: Application.Resources.IUser;
                if (d != null && d.length > 0) {
                    user = d[0];
                };
                callback(user);
            },
            function (r: any) {
                callback(null, r);
            });
            
        }

        public GetUser(id: number, callback: Function) {
            this._userClass.get({ id: id }, function (d: Application.Resources.IUser, h: any) {
                var user: Application.Resources.IUser;
                if (d != null) {
                    user = d;
                };
                callback(user);
            },
            function (r: any) {
                callback(null, r);
            });
        }

        public GetUsersByLogin(login: string, callback: Function) {
            this._userClass.query({ login: login }, function (d: Application.Resources.IUser[], h: any) {
                var users: Application.Resources.IUser[];
                if (d != null && d.length > 0) {
                    users = d;
                };
                callback(users);
            },
            function (r: any) {
                callback(null, r);
            });
        }

        public GetUsers(callback: Function) {
            this._userClass.query(
            function (d: Application.Resources.IUser[], h: any) {
                var users: Application.Resources.IUser[];
                if (d != null && d.length > 0) {
                    users = d;
                };
                callback(users);
            },
            function (err: any) {
                callback(null, err);
            });
        }

        public PostUser(user: any, callback: Function) {
            this._userClass.save(null, user
                , function success(data: Application.Resources.IUser, h: any) {
                    callback(data, null);
                }
                , function failure(err: any) {
                    callback(null, err);
                });
        }

        public UpdateUser(user: any, callback: Function) {
            this._userClass.save({ id: user.user_id }, user
                , function success(data: Application.Resources.IUser, h: any) {
                    callback(data, null);
                }
                , function failure(err: any) {
                    callback(null, err);
                });
        }
    };
}